//
//  YXTestDelegateViewController.m
//  YXImageBrowserDemo
//
//  Created by guo peng huang on 2021/5/19.
//

#import "YXTestDelegateViewController.h"
#import "YXPhotoAlbumManager.h"
#import "YXVideoData.h"
#import "YXImageData.h"
#import "YXImageBrowser.h"

@interface YXTestDelegateViewController () <YXImageBrowserDataSource>

@end

@implementation YXTestDelegateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

+ (NSString *)yx_title {
    return @"用代理配置数据源（以相册为例）";
}

- (void)initData {
    [YXPhotoAlbumManager getPhotoAlbumAuthorizationSuccess:^{
        self.dataArray = [YXBaseFileManager yx_imagePHAssets];
    } failed:^{
        
    }];
}

- (void)yx_selectedIndex:(NSInteger)index {
    YXImageBrowser *browser = [YXImageBrowser new];
    browser.dataSource = self;
    browser.currentPage = index;
    [browser show];
}

- (NSInteger)yx_numberOfCellsInImageBrowser:(YXImageBrowser *)imageBrowser {
    return self.dataArray.count;
}

- (id<YXDataProtocol>)yx_imageBrowser:(YXImageBrowser *)imageBrowser dataForCellAtIndex:(NSInteger)index {
    PHAsset *asset = (PHAsset *)self.dataArray[index];
    if (asset.mediaType == PHAssetMediaTypeVideo) {

        // 系统相册的视频
        YXVideoData *data = [YXVideoData new];
        data.videoPHAsset = asset;
        data.projectiveView = [self viewAtIndex:index];
        return data;

    } else if (asset.mediaType == PHAssetMediaTypeImage) {

        // 系统相册的图片
        YXImageData *data = [YXImageData new];
        data.imagePHAsset = asset;
        data.projectiveView = [self viewAtIndex:index];
        return data;

    } else
    
    return nil;
}

@end
